/*
 ------------------------------------------------------------------------------
 Copyright (C) 2013 Eternal Games.

 This file is part of the EternalTech source code.

 The EternalTech source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The EternalTech source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the EternalTech source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


//
// anim_animator.cpp - animator manager
//


#include "anim_local.h"


static animator_t *		anim_animators[MAX_ANIMATORS];
static int				anim_numAnimators;


/*
 ==================
 
 ==================
*/
void Anim_SetAnimation (animator_t *animator, animation_t *animation){

}


// ============================================================================


/*
 ==================
 Anim_AllocAnimator
 ==================
*/
animator_t *Anim_AllocAnimator (){

	animator_t	*animator;
	int			i;

	// Try to find a free slot
	for (i = 0; i < anim_numAnimators; i++){
		if (!anim_animators[i])
			break;
	}

	if (i == anim_numAnimators){
		if (anim_numAnimators == MAX_ANIMATORS)
			G_Error("Anim_AllocAnimator: MAX_ANIMATORS hit");

		anim_numAnimators++;
	}

	// Allocate the animator
	anim_animators[i] = animator = (animator_t *)glqMemory->Alloc(sizeof(animator_t), game.memoryTag);

	// Fill it in
	animator->index = i;

	Anim_SetAnimation(animator, NULL);

	return animator;
}

/*
 ==================
 Anim_FreeAnimator
 ==================
*/
void Anim_FreeAnimator (animator_t *animator){

	if (!animator)
		return;

	// Free the slot
	if (animator->index < 0 || animator->index >= MAX_ANIMATORS)
		G_Error("Anim_FreeAnimator: index out of range");

	anim_animators[animator->index] = NULL;

	// Free the animator
	glqMemory->Free(animator);
}

/*
 ==================
 Anim_FreeAllAnimators
 ==================
*/
static void Anim_FreeAllAnimators (){

	animator_t	*animator;
	int			i;

	// Free all the animators
	for (i = 0; i < anim_numAnimators; i++){
		animator = anim_animators[i];
		if (!animator)
			continue;

		glqMemory->Free(animator);
	}

	// Clear animator list
	glqMemory->Fill(anim_animators, 0, sizeof(anim_animators));

	anim_numAnimators = 0;
}


/*
 ==============================================================================

 INITIALIZATION & SHUTDOWN

 ==============================================================================
*/


/*
 ==================

 ==================
*/
void Anim_Init (){

}

/*
 ==================

 ==================
*/
void Anim_Shutdown (){

	// Free all the animators
	Anim_FreeAllAnimators();
}